package com.java2020.online_mall.controller;

import com.alibaba.druid.util.StringUtils;
import com.google.gson.Gson;
import com.java2020.online_mall.model.OrderRequestParam;
import com.java2020.online_mall.model.OrderResponse;
import com.java2020.online_mall.model.Result;
import com.java2020.online_mall.service.OrderService;
import com.java2020.online_mall.service.OrderServiceImpl;
import com.java2020.online_mall.utils.HttpUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @PackgeName: ${PACKAGE_NAME}
 * @Author: MNHK
 * Date: 2020/5/2 21:40
 * @Version:1.8
 */

@WebServlet("/api/admin/order/*")
public class OrderServlet extends HttpServlet {

    Gson gson = new Gson();
    private OrderService orderService = new OrderServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String requestURI = request.getRequestURI();
        String correct = requestURI.replace("/api/admin/order/", "");
        if ("orderByPage".equals(correct)) {
            orderByPage(request,response);
        }
    }

    private void orderByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String requestBody = HttpUtils.getRequestBody(request);
        OrderRequestParam param = gson.fromJson(requestBody, OrderRequestParam.class);
        try {
            if(!StringUtils.isEmpty(param.getMoneyLimit1())){
                Double.parseDouble(param.getMoneyLimit1());
            }
            if(!StringUtils.isEmpty(param.getMoneyLimit2())){
                Double.parseDouble(param.getMoneyLimit2());
            }
        }catch (Exception e){
            return;
        }
        OrderResponse orderResponse = orderService.ordersByPage(param);
        Result result = new Result();
        result.setCode(0);
        result.setData(orderResponse);
        response.getWriter().println(gson.toJson(result));
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
